﻿using Common;
using Common.Base;
using Common.Class;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using Extra.ERP;
using Security.ERP;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace Security.Security
{
    public class xucGroupAdd : xucBaseAdd
    {
        private CheckEdit chxUse;
        private IContainer components = null;
        private LabelControl lblDescription;
        public string m_Group_Id;
        public string m_Group_Name;
        private TextEdit txtDescription;

        public event SuccessEventHander Success;

        public xucGroupAdd()
        {
            this.InitializeComponent();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            this.txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected void Init()
        {
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucGroupAdd));
            this.lblDescription = new LabelControl();
            this.txtDescription = new TextEdit();
            this.chxUse = new CheckEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize)base.Err).BeginInit();
            this.txtDescription.Properties.BeginInit();
            this.chxUse.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(this.txtDescription);
            base.gcInfo.Controls.Add(this.lblDescription);
            base.gcInfo.Controls.Add(this.chxUse);
            base.gcInfo.Size = new Size(0x117, 0x7f);
            base.gcInfo.Controls.SetChildIndex(this.chxUse, 0);
            base.gcInfo.Controls.SetChildIndex(this.lblDescription, 0);
            base.gcInfo.Controls.SetChildIndex(this.txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.txtNAME.Location = new Point(0x3e, 0x34);
            base.txtNAME.Size = new Size(0xd4, 20);
            base.lblName.Location = new Point(3, 0x37);
            base.txtID.Location = new Point(0x3e, 0x1a);
            base.txtID.Size = new Size(0xd4, 20);
            base.imageCollection2.ImageStream = (ImageCollectionStreamer)manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x59, 0x86);
            base.btnSave.Size = new Size(0x5f, 0x1b);
            base.btnCancel.Location = new Point(0xb9, 0x86);
            base.btnSaveNew.Location = new Point(1, 0x86);
            this.lblDescription.Location = new Point(6, 0x4f);
            this.lblDescription.Name = "lblDescription";
            this.lblDescription.Size = new Size(40, 13);
            this.lblDescription.TabIndex = 4;
            this.lblDescription.Text = "Diễn giải";
            this.txtDescription.Location = new Point(0x3e, 0x4c);
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new Size(0xd4, 20);
            this.txtDescription.TabIndex = 5;
            this.chxUse.EditValue = true;
            this.chxUse.Location = new Point(60, 0x66);
            this.chxUse.Name = "chxUse";
            this.chxUse.Properties.Caption = "C\x00f2n quản l\x00fd";
            this.chxUse.Size = new Size(0x5f, 0x12);
            this.chxUse.TabIndex = 6;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucGroupAdd";
            base.Size = new Size(0x119, 0xa4);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize)base.Err).EndInit();
            this.txtDescription.Properties.EndInit();
            this.chxUse.Properties.EndInit();
            base.ResumeLayout(false);
        }

        public void RaiseSuccessEventHander(SYS_GROUP Item)
        {
            if (this.Success != null)
            {
                this.Success(this, Item);
            }
        }

        public void SetData(SYS_GROUP item)
        {
            base.txtID.Text = item.Group_ID;
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.Group_Name;
            this.txtDescription.Text = item.Description;
            this.chxUse.Checked = item.Active;
            SYS_LOG.Insert("Vai Tr\x00f2 Người D\x00f9ng", "Xem", item.Group_ID);
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            TextEdit control = (TextEdit)sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                SYS_GROUP sys_group = new SYS_GROUP();
                if (sys_group.Exist(control.Text))
                {
                    base.Err.SetError(control, "ID is exist.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            TextEdit control = (TextEdit)sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                SYS_GROUP sys_group = new SYS_GROUP();
                if ((base.m_Status == Actions.Add) && sys_group.Exist(control.Text))
                {
                    base.Err.SetError(control, "ID is exist.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            string str = string.Empty;
            SYS_GROUP item = new SYS_GROUP();
            if (str == "OK")
            {
                this.RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.IsAdd("bbiUserGroup"))
            {
                SYS_LOG.Insert("Vai Tr\x00f2 Người D\x00f9ng", "Th\x00eam", base.txtID.Text);
                base.CreateWaitDialog();
                base.SetWaitDialogCaption("Đang lưu dữ liệu...");
                Cursor.Current = Cursors.WaitCursor;
                string str = string.Empty;
                SYS_GROUP item = new SYS_GROUP(base.txtID.Text, base.txtNAME.Text, base.txtNAME.Text, this.txtDescription.Text, this.chxUse.Checked);
                str = item.Insert();
                if (str == "OK")
                {
                    this.RaiseSuccessEventHander(item);
                    this.m_Group_Id = base.txtID.Text;
                    this.m_Group_Name = base.txtNAME.Text;
                    Cursor.Current = Cursors.Default;
                    this.DoHide();
                    return str;
                }
                XtraMessageBox.Show("Lỗi:\n\t" + str, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.DoHide();
            }
            return "";
        }

        protected override string uc_Update()
        {
            if (!MyRule.IsEdit("bbiUserGroup"))
            {
                SYS_LOG.Insert("Vai Tr\x00f2 Người D\x00f9ng", "Cập Nhật", base.txtID.Text);
                base.CreateWaitDialog();
                base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
                string str = string.Empty;
                SYS_GROUP item = new SYS_GROUP(base.txtID.Text, base.txtNAME.Text, base.txtNAME.Text, this.txtDescription.Text, this.chxUse.Checked);
                str = item.Update();
                if (str == "OK")
                {
                    this.RaiseSuccessEventHander(item);
                    this.DoHide();
                    return str;
                }
                XtraMessageBox.Show("Lỗi:\n\t" + str, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.DoHide();
            }
            return "";
        }

        public delegate void SuccessEventHander(object sender, SYS_GROUP Item);
    }

}